#import "LayerSetPos.h"
#import "LayerMain.h"
#import "LayerLogo.h"
#import "SimpleAudioEngine.h"

@implementation SceneSetPos

-(id)init {
	if ((self = [super init])) {
		[self addChild:[LayerSetPos node]];
	}
	return self;
}

-(void)dealloc {
	[super dealloc];
}

@end


@implementation LayerSetPos

- (id) init {
	if ((self = [super init])) 	{
		CGSize size = [[CCDirector sharedDirector] winSize];

		// Background
		CCSprite* bg = [CCSprite spriteWithFile:[NSString stringWithFormat:@"Bg%d.png",arc4random()%6+1]];
		[bg setPosition:ccp(size.width/2, size.height/2)];
		[self addChild:bg];
		
		// Menu
		// Mode
		CCSprite* mode = [CCSprite spriteWithFile:@"Mode.png"];
		[mode setPosition:ccp(50, 450)];
		[self addChild:mode];
		 
		 CCMenuItemImage* singleMode = [CCMenuItemImage itemFromNormalImage:@"Single.png"
															 selectedImage:@"SingleSel.png"];
		 CCMenuItemImage* doubleMode = [CCMenuItemImage itemFromNormalImage:@"Double.png"
															 selectedImage:@"DoubleSel.png"];
		 mMenuFinger = [CCMenuItemToggle itemWithTarget:self
											   selector:@selector(changeFinger)
												  items:singleMode,doubleMode,nil];
		 [mMenuFinger setPosition:ccp(200, 450)];
		 
		// Time
		CCSprite* time = [CCSprite spriteWithFile:@"Time.png"];
		[time setPosition:ccp(50, 400)];
		[self addChild:time];
		
		CCMenuItemImage* time10Secs = [CCMenuItemImage itemFromNormalImage:@"10Sec.png"
															 selectedImage:@"10SecSel.png"];
		CCMenuItemImage* time20Secs = [CCMenuItemImage itemFromNormalImage:@"20Sec.png"
															 selectedImage:@"20SecSel.png"];
		CCMenuItemImage* time30Secs = [CCMenuItemImage itemFromNormalImage:@"30Sec.png"
															 selectedImage:@"30SecSel.png"];
		CCMenuItemImage* time60Secs = [CCMenuItemImage itemFromNormalImage:@"60Sec.png"
															 selectedImage:@"60SecSel.png"];
		mMenuTime = [CCMenuItemToggle itemWithTarget:self
											selector:@selector(changeTime)
											   items:time10Secs,time20Secs,time30Secs,time60Secs,nil];
		[mMenuTime setPosition:ccp(200, 400)];

		// Start
		CCMenuItemImage* start = [CCMenuItemImage itemFromNormalImage:@"Start.png"
														selectedImage:@"StartSel.png"
															   target:self
															 selector:@selector(start)];
		[start setPosition:ccp(240, 30)];
		
		// Back
		CCMenuItemImage* back = [CCMenuItemImage itemFromNormalImage:@"Back.png"
													   selectedImage:@"BackSel.png"
															  target:self
															selector:@selector(back)];
		[back setPosition:ccp(80,30)];

		CCMenu * menu = [CCMenu menuWithItems:mMenuFinger,mMenuTime,start,back,nil];
		[menu setPosition:ccp(0, 0)];
		[self addChild:menu];
		
		// Circle
		mCircle1 = [CCSprite spriteWithFile:@"TapBtn.png"];
		[mCircle1 setPosition:ccp(160, kCircleY)];
		[self addChild:mCircle1];
		
		mCircle2 = [CCSprite spriteWithFile:@"TapBtn.png"];
		[mCircle2 setPosition:ccp(160, kCircleY)];
		[self addChild:mCircle2];
		[mCircle2 setVisible:NO];
	}
	return self;
}

-(void)changeFinger {
	[[SimpleAudioEngine sharedEngine] playEffect:@"Menu.wav"];
	if ([mMenuFinger selectedIndex] == 0) {
		[mCircle2 runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.1 position:ccp(160,kCircleY)],
							 [CCHide action],nil]];
		[mCircle1 runAction:[CCMoveTo actionWithDuration:0.1 position:ccp(160, kCircleY)]];
	}
	else {
		[mCircle2 runAction:[CCSequence actions:[CCShow action],
							 [CCMoveTo actionWithDuration:0.1 position:ccp(240, kCircleY)],nil]];
		[mCircle1 runAction:[CCMoveTo actionWithDuration:0.1 position:ccp(80, kCircleY)]];
	}
}

-(void)changeTime {
	[[SimpleAudioEngine sharedEngine] playEffect:@"Menu.wav"];
}

-(void)start {
	[[SimpleAudioEngine sharedEngine] playEffect:@"Menu.wav"];
	SceneMain* s = [[[SceneMain alloc] initWithMode:[mMenuFinger selectedIndex]
											   time:[mMenuTime selectedIndex]] autorelease];
	[[CCDirector sharedDirector] replaceScene:s];
}

-(void)back {
	[[SimpleAudioEngine sharedEngine] playEffect:@"Menu.wav"];
	[[CCDirector sharedDirector] replaceScene:[LayerLogo node]];
}

-(void)dealloc {
	[super dealloc];
}

@end
